package cn.luke.basic.controller;

import cn.dev33.satoken.annotation.SaCheckLogin;
import cn.dev33.satoken.annotation.SaCheckPermission;
import cn.luke.basic.entity.DtoRole;
import cn.luke.basic.log_api.LogApi;
import cn.luke.basic.rs.ResponseResult;
import cn.luke.basic.services.ServiceRole;
import cn.luke.basic.utils.MyAssert;
import com.baomidou.mybatisplus.core.metadata.IPage;
import jakarta.annotation.Resource;
import org.springframework.web.bind.annotation.PostMapping;
import org.springframework.web.bind.annotation.RequestBody;
import org.springframework.web.bind.annotation.RequestMapping;
import org.springframework.web.bind.annotation.RestController;
import java.util.Map;

/**
 * Created by Luke on 2025/3/7 16:58.
 */
@RestController
@ResponseResult
@SaCheckLogin
@RequestMapping("/role")
public class ControllerRole implements MyAssert {

    @Resource
    private ServiceRole serviceRole;

    /**
     * 创建角色
     * @param role roleName,remark
     */
    @LogApi("角色-创建")
    @PostMapping("/add")
    @SaCheckPermission("role.add")
    public void register(@RequestBody DtoRole role) {
        String username = strIsNull(role.getRoleName(), "角色名称不能为空");
        serviceRole.doAdd(username, role.getRemark());
    }

    /**
     * 更新角色信息
     * @param role id,roleName,remark
     */
    @LogApi("角色-编辑")
    @PostMapping("/update")
    @SaCheckPermission("role.update")
    public void update(@RequestBody DtoRole role) {
        Long id = (long) idIsNull(role.getId(), "角色ID不能为空");
        String username = strIsNull(role.getRoleName(), "角色名称不能为空");
        serviceRole.doUpdate(id, username, role.getRemark());
    }

    /**
     * 删除角色
     * @param params id
     */
    @LogApi("角色-删除")
    @PostMapping("/delete")
    @SaCheckPermission("role.delete")
    public void delete(@RequestBody Map<String, Object> params) {
        Long id = (long) idIsNull(params.get("id"), "角色ID不能为空");
        String roleName = strIsNull(params.get("roleName"), "角色名不能为空");
        serviceRole.doDelete(id, roleName);
    }

    /**
     * 分页查询角色列表
     * @param params key,pageNum,pageSize
     * @return IPage<DtoRole>
     */
    @PostMapping("/page")
    @SaCheckPermission("role.search")
    public IPage<DtoRole> get(@RequestBody Map<String, Object> params) {
        pageIsNull(params);
        return serviceRole.findPageByParams(params);
    }

}
